﻿using AutoMapper;
using B.S.Unit.Domain;
using B.S.Unit.ErrorCode;
using B.S.Unit.Infacet;
using B.S.Unit.Write.API.Applications.Command;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.Business.Write.API.Applications.CommandHandler
{
    public class UnitUpdCommandHandler : IRequestHandler<UnitUpdCommand, UnitResult<object>>
    {
        private readonly IBaseRepository<UnitApp> UnitApp;
        private readonly IMapper mapper;

        public UnitUpdCommandHandler(IBaseRepository<UnitApp> UnitApp, IMapper mapper)
        {
            this.UnitApp = UnitApp;
            this.mapper = mapper;
        }
        public Task<UnitResult<object>> Handle(UnitUpdCommand request, CancellationToken cancellationToken)
        {
            UnitResult<object> result = new UnitResult<object>();
            var m = mapper.Map<UnitApp>(request);

            m.UnitAppCode = YitIdHelper.NextId().ToString();
            m.UnitAppName = request.UnitAppName;
           
           
            m.UnitDescription = request.UnitDescription;

            var a = UnitApp.Update(m);
            if (a > 0)
            {
                result.Code = UnitCode.修改成功;
                result.Msg = "修改成功";
                result.Data = a;

            }
            else
            {
                result.Code = UnitCode.修改成功;
                result.Msg = "修改成功";
                result.Data = a;
            }
            return Task.FromResult(result);
        }
    }

}
